﻿Imports TBCC.UtilityMobile
Imports System.Threading

Public Class TerminalShowForm

    Private _pi As ProjectItem

    Private _NetID As Byte = g_netID

    Private Const _Offset As UShort = 6000
    Private _Length As UShort = 4              '4 * 2 (Byte)
    Private Const _AlarmLenRead As UShort = 2   '2 * 1 (Byte); 高8位,低8位,高8位,低8位
    Private Const _AlarmLenUsed As UShort = 1   '1/2 Byte

    Private buf() As Byte
    Private Const _AlarmFlagOn As Integer = 0
    Private Const _AlarmFlagOff As Integer = 1
    Private Const _AlarmNormal As String = "正常"
    Private Const _AlarmHigh As String = "超高"
    Private Const _AlarmLow As String = "超低"
    Private ReadOnly _AlarmHighMask() As Integer = {1, 4, 16, 64}
    Private ReadOnly _AlarmLowMask() As Integer = {2, 8, 32, 128}

    'T1H    T1L     T2H     T2L     T3H     T3L     RH1H	RH1L
    '0	    1	    2	    3	    4	    5	    6	    7
    '1	    2	    4	    8	    16	    32	    64	    128

    Private _WorkThread As Thread
    Private _IsWorking As Boolean
    Private Const _RefreshInterval As UShort = 1000

#Region " 构造函数 "

    Private _valCtrls(3) As TextBox
    Private _armCtrls(3) As Label

    Public Sub New()

        ' 此调用是 Windows 窗体设计器所必需的。
        InitializeComponent()

        ' 在 InitializeComponent() 调用之后添加任何初始化。
        _valCtrls(0) = Me.txtTemp1 : Me.txtTemp1.Tag = 0
        _valCtrls(1) = Me.txtTemp2 : Me.txtTemp2.Tag = 1
        _valCtrls(2) = Me.txtTemp3 : Me.txtTemp3.Tag = 2
        _valCtrls(3) = Me.txtHumi1 : Me.txtHumi1.Tag = 3
        _armCtrls(0) = Me.labAlarm1
        _armCtrls(1) = Me.labAlarm2
        _armCtrls(2) = Me.labAlarm3
        _armCtrls(3) = Me.labAlarm4
    End Sub

#End Region

    Private Sub TerminalShowForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Me.CenterToParent()

        _pi = Me.MyParentForm.PI

        Select Case _pi.ProjectType
            Case ProjectType_Enum.Car '移动车载
                Exit Select

            Case ProjectType_Enum.Box '小批零
                Me.pnlTemp2.Enabled = False
                Me.pnlTemp3.Enabled = False
                Me.pnlHumi1.Enabled = False
                Exit Select
        End Select

        _IsWorking = True
        _WorkThread = New Thread(AddressOf ThreadWorker)
        _WorkThread.IsBackground = True
        _WorkThread.Start()

    End Sub

    Private Sub TerminalShowForm_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        _IsWorking = False
        _WorkThread = Nothing
    End Sub

    '工作线程
    Private Sub ThreadWorker()

        Do While _IsWorking

            Try

                If RefreshData() = True Then
                    UpdateData2Ctrl()
                Else
                    ClearData4Ctrl()
                End If

            Catch ex As Exception
                Debug.WriteLine(ex.Message)
            End Try

            System.Threading.Thread.Sleep(_RefreshInterval)
        Loop

    End Sub

    '刷新端口数据
    Private Function RefreshData() As Boolean
        Try

            buf = COMHelper.Instance.Send4Byte(_NetID, FunctionCode.WordReadonly, _Offset, _AlarmLenRead + (_Length + 2) * 2)

        Catch ex As Exception
            Debug.WriteLine(ex.Message)
            Return False
        End Try

        If buf Is Nothing Then
            Return False
        End If

        Return True
    End Function

    '更新数据至界面
    Private Sub UpdateData2Ctrl()
        If Me.InvokeRequired = True Then
            Me.Invoke(New CrossAppDomainDelegate(AddressOf _UpdateData2Ctrl))
        Else
            _UpdateData2Ctrl()
        End If
    End Sub
    Private Sub _UpdateData2Ctrl()
        Dim byteAlarm As Byte : Dim byteState As Byte : Dim byteStateL As Byte
        byteAlarm = buf(1)      '获取第一个低8位; 一共8个报警标志; 0:字节1高8位,1:字节1低8位,2:字节2高8位,3:字节2低8位

        Dim txt As TextBox : Dim bufSingle(3) As Byte : Dim value As Single : Dim lab As Label
        For i As Integer = 0 To _Length - 1 '报警与AI数据
            lab = _armCtrls(i) : txt = _valCtrls(i)
            System.Array.Copy(buf, (_AlarmLenRead * 2) + (txt.Tag * 4), bufSingle, 0, bufSingle.Length)
            value = NumericConvert.Byte2Single(bufSingle)
            If Math.Abs(value - INVALID_DATA) <= 0.001 Then '无效数据
                txt.Text = "--"
                lab.Image = My.Resources.AlarmOff
                lab.ForeColor = SystemColors.ControlText
                lab.Text = "未知"
            Else
                txt.Text = value.ToString("0.00")
                If (byteAlarm And _AlarmHighMask(txt.Tag)) = 0 Then       '超高; 1:正常; 0:报警
                    lab.Image = My.Resources.AlarmOn
                    lab.ForeColor = Color.Red
                    lab.Text = _AlarmHigh
                ElseIf (byteAlarm And _AlarmLowMask(txt.Tag)) = 0 Then    '超低
                    lab.Image = My.Resources.AlarmOn
                    lab.ForeColor = Color.Green
                    lab.Text = _AlarmLow
                Else                                                    '正常
                    lab.Image = My.Resources.AlarmOff
                    lab.ForeColor = SystemColors.ControlText
                    lab.Text = _AlarmNormal
                End If
            End If
        Next


        byteState = buf(3) '状态信息
        byteStateL = buf(2)   '维度信息状态

        For i As Integer = 0 To 1 'GPS数据
            If i = 0 Then
                txt = LongitudeTb
            Else
                txt = LatitudeTb
            End If

            System.Array.Copy(buf, (_AlarmLenRead * 2) + ((_Length + i) * 4), bufSingle, 0, bufSingle.Length)
            value = NumericConvert.Byte2Single(bufSingle)  '经纬度

            If i = 0 Then '经度
                txt.Text = Program.Gps_GetFormatLongitude(value, byteState)
            Else  '纬度
                txt.Text = Program.Gps_GetFormatLatitude(value, byteStateL)
            End If
        Next

    End Sub

    '清除数据从界面
    Private Sub ClearData4Ctrl()
        If Me.InvokeRequired = True Then
            Me.Invoke(New CrossAppDomainDelegate(AddressOf _ClearData4Ctrl))
        Else
            _ClearData4Ctrl()
        End If
    End Sub
    Private Sub _ClearData4Ctrl()
        Dim txt As TextBox : Dim lab As Label
        For i As Integer = 0 To _Length - 1
            txt = _valCtrls(i)
            txt.Text = "无法读取"
            lab = _armCtrls(i)
            lab.Image = My.Resources.AlarmOff
            lab.ForeColor = SystemColors.ControlText
            lab.Text = "未知"
        Next

        LongitudeTb.Text = "无法读取"
        LatitudeTb.Text = "无法读取"
    End Sub

End Class